package com.ita.fileutil;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Queue;

public class FileWriterT implements Runnable{
	
	private Queue<Student> queue;

	public FileWriterT(Queue<Student> queue) {
		super();
		this.queue = queue;
	}


	void writeFile(){
		try{
			OutputStream outputStream =new FileOutputStream("./files/student2.txt");
			while (true) {
				synchronized (queue) {
					if (queue.size() > 0) {
						Student student = queue.remove();
						String dt = student.getSname() + "\t" + student.getNum() + "\t" + student.getAge() + "\n";
						outputStream.write(dt.getBytes());
					}else{
						queue.notify();
						queue.wait();
					}
				}
				Thread.sleep(10000);
				if(queue.size() == 0){
					break;
				}
			}
			outputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		writeFile();
	}
	
	
}
